package com.play.ballen.pack.ui

import androidx.fragment.app.Fragment
import com.flyco.tablayout.listener.OnTabSelectListener
import com.play.ballen.R
import com.play.ballen.databinding.FragmentPackBinding
import com.play.ballen.event.EventCost
import com.play.ballen.mall.entity.MallBallLevel
import com.play.ballen.me.dialog.BottomSheetDialogUtils
import com.play.ballen.view.PackChildDestroyHeaderView
import com.yuven.appframework.adapter.BaseViewPagerAdapter
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.component.BaseMVVMFragment
import com.yuven.baselib.component.eventbus.Event
import com.yuven.baselib.component.viewmodel.BaseViewModel.EmptyVM
import com.yuven.baselib.utils.ResourceUtil
import com.yuven.baselib.utils.ViewUtils

class PackFragment : BaseMVVMFragment<EmptyVM, FragmentPackBinding>() {

    private var isLoadData = false

    private val fragmentOwner by lazy {
        PackChildFragment.newInstance(TYPE_PACK_OWNER)
    }
    private val fragmentSale by lazy {
        PackChildFragment.newInstance(TYPE_PACK_SALE)
    }
    private val fragmentDestroy by lazy {
        PackChildFragment.newInstance(TYPE_PACK_DESTROY)
    }
    private val fragmentLapsed by lazy {
        PackChildFragment.newInstance(TYPE_PACK_LAPSED)
    }

    private val fragmentList by lazy {
        arrayListOf<Fragment>(fragmentOwner, fragmentSale, fragmentDestroy, fragmentLapsed)
    }

    private val headerViewDestroy = PackChildDestroyHeaderView()

    private lateinit var adapter: BaseViewPagerAdapter
    private var listLevel: List<MallBallLevel>? = null
    private var setLevelText = false

    private val titles = arrayOf(
        ResourceUtil.getString(R.string.pack_tab_owner),
        ResourceUtil.getString(R.string.pack_tab_saling),
        ResourceUtil.getString(R.string.pack_tab_destory),
        ResourceUtil.getString(R.string.pack_tab_lapsed),
    )

    private val title1 = arrayListOf(
        ResourceUtil.getString(R.string.pack_tab_owner),
        ResourceUtil.getString(R.string.pack_tab_saling),
        ResourceUtil.getString(R.string.pack_tab_destory),
        ResourceUtil.getString(R.string.pack_tab_lapsed),
    )
    
    private var selectLevelId = ""

    private val levelDialogUtils by lazy {
        BottomSheetDialogUtils<MallBallLevel>(mContext)
    }

    companion object {
        val TAG: String = PackFragment::class.java.simpleName

        const val TYPE_PACK_OWNER = "1"
        const val TYPE_PACK_SALE = "2"
        const val TYPE_PACK_DESTROY = "-1"
        const val TYPE_PACK_LAPSED = "0"

        fun newInstance(): PackFragment {
            return PackFragment()
        }
    }

    override fun initIntent() {}

    override fun layoutId(): Int {
        return R.layout.fragment_pack
    }

    override fun viewModelClass(): Class<EmptyVM> {
        return EmptyVM::class.java
    }

    override fun initView() {
        dataBinding.fakeStatusBar.layoutParams.height = ViewUtils.getStatusHeight()

        dataBinding.llLevel.setOnSingleClickListener {
            levelDialogUtils.show(listLevel)
        }
    }

    override fun initData() {
        fragmentOwner.apply {
            levelCallBack = {
                //首次才默认取第一个
                if (!setLevelText && !it.isNullOrEmpty()) {
                    dataBinding.tvLevel.text = it[0].getItemString()
                    setLevelText = true
                }

                listLevel = it
            }
            getSelectLevelId = {
                selectLevelId
            }
        }

        fragmentSale.apply {
            getSelectLevelId = {
                selectLevelId
            }
        }

        fragmentDestroy.apply {
            getSelectLevelId = {
                selectLevelId
            }

            headerViewDestroy.registerView(mContext)
            headerView = headerViewDestroy
        }

        fragmentLapsed.apply {
            getSelectLevelId = {
                selectLevelId
            }
        }

        levelDialogUtils.apply {
            itemCallback = tag@{ select ->
                if (select.id == selectLevelId) {
                    return@tag
                }

                selectLevelId = select.id
                dataBinding.tvLevel.text = select.getItemString()

                fragmentOwner.notifyLevelChange()
                fragmentSale.notifyLevelChange()
                fragmentDestroy.notifyLevelChange()
                fragmentLapsed.notifyLevelChange()
            }
        }
    }

    override fun registerEventBus(): Boolean {
        return true
    }

    override fun handleEvent(event: Event<Any>) {
        super.handleEvent(event)
        when (event.code) {
            EventCost.CODE_REFRESH_PACK_SALE -> {
                fragmentSale.notifyLevelChange()
            }

            EventCost.CODE_REFRESH_PACK_OWNER -> {
                fragmentOwner.notifyLevelChange()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (!isLoadData) {
            adapter = BaseViewPagerAdapter(childFragmentManager, fragmentList, title1)
            dataBinding.viewPager.adapter = adapter
            dataBinding.viewPager.offscreenPageLimit = fragmentList.size
//            dataBinding.segmentTabLayout.set(dataBinding.viewPager)
            dataBinding.segmentTabLayout.setTabData(titles)
            dataBinding.segmentTabLayout.setOnTabSelectListener(object : OnTabSelectListener {
                override fun onTabSelect(position: Int) {
                    // Handle tab selection here
                    dataBinding.viewPager.currentItem = position
                }

                override fun onTabReselect(position: Int) {
                    // Handle tab reselection here if needed
                }
            })
            isLoadData = true
        }
    }

}